using UnityEngine;
using UnityEngine.UI;

public class EnemyAnger_UI : MonoBehaviour
{
    public EnemyAnger enemyAnger;
    public Slider slider;
    public Image face;
    public Sprite[] faceSprites;
    public Sprite faceGameOver;

    private void OnEnable()
    {
        if (GameManager.Instance != null)
        {
            GameManager.Instance.GameOverEvent.AddListener(GameOver);
        }
    }

    private void OnDisable()
    {
        if(GameManager.Instance != null)
        {
            GameManager.Instance.GameOverEvent.RemoveListener(GameOver);
        }
    }
    

    private void GameOver(int arg0)
    {
        if(arg0 == 0) face.sprite = faceGameOver;
    }

    void Update()
    {
        if (GameManager.Instance.IsGameOver) return;
        slider.value = enemyAnger.EnemyAngerBarPercent;
        face.sprite = GetFace();
    }

    Sprite GetFace()
    {
        if (face.sprite != faceSprites[enemyAnger.StarCount - 1])
        {
            AudioManager.Instance.PlaySFX(8 + enemyAnger.StarCount);
        }
        return faceSprites[enemyAnger.StarCount - 1];
    }
}
